<?php
/**
 * Created by PhpStorm.
 * User: zhoubinbin
 * Date: 2018/2/22
 * Time: 上午9:27
 */

namespace app\admin\controller;


class Coupon extends Controller
{
    // public function __construct()
    // {
        // parent::__construct();
        // $this->CouponModel = D('Coupon');
        // $this->CouponTypeModel = D('CouponType');
    // }

    public function orderType()
    {
        if (IS_POST) {
            $json = [
                ['id' => 9, 'text' => '通用'],
                ['id' => 0, 'text' => '日常代驾'],
                ['id' => 1, 'text' => '商务车'],
                ['id' => 3, 'text' => '出租车'],
                ['id' => 4, 'text' => '顺风车']
            ];
            $this->ajaxReturn($json);
        }
    }

    public function type()
    {
        if (IS_POST) {
            $json = [
                ['id' => 1, 'text' => '注册优惠券'],
                ['id' => 2, 'text' => '普通优惠券']
            ];
            $this->ajaxReturn($json);
        }
    }

    // 优惠券列表
    public function listCouponType()
    {
        if (IS_POST) {
            $json = $this->CouponTypeModel->where(['status' => 1])->select();
            $this->ajaxReturn($json);
        }
        $this->display();
    }

    // 添加优惠券类型
    public function addCouponType()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['type'] = '1';
            $data['expirationType'] = '2';
            $data['cid'] = 1;
            $data['status'] = 1;
            $data['created_at'] = time();
            $data['updated_at'] = time();
            $status = $this->CouponTypeModel->add($data);
            $msg = doReturn("添加成功", "添加失败", $status);
            $this->ajaxReturn($msg);
        }
        $this->display();
    }

    // 编辑优惠券类型
    public function editCouponType()
    {
        if (IS_POST) {

        }
    }

    // 删除优惠券类型
    public function delCouponType()
    {
        if (IS_POST) {

        }
    }

    // 优惠券列表
    public function listCoupon()
    {
        if (IS_POST) {
            $data = [
                "total" => $this->CouponModel->where(['status' => I('get.status')])->count(),
                "rows" => $this->CouponModel->relation(true)->where(['status' => I('get.status')])->select()
            ];
            $this->ajaxReturn($data);
        }
        $this->display();
    }

    // 添加优惠券
    public function addCoupon()
    {
        if (IS_POST) {
            $data = I('post.');
            $CouponTypeInfo = $this->CouponTypeModel->find($data['typeId']);
            if ($CouponTypeInfo['expirationType'] == 1) {
                $expiration = strtotime($CouponTypeInfo['expirationValue']);
            } else {
                $expiration = '';
            }
            $insert_arr = [];
            for ($i = 0; $i < $data['num']; $i++) {
                $arr['uid'] = 0;
                $arr['code'] = strtoupper(md5(microtime(true) . $i . randString($i)));;
                $arr['status'] = 1;
                $arr['type'] = $data['typeId'];
                $arr['expiration'] = 0;
                $arr['receive_at'] = 0;
                $arr['use_at'] = 0;
                $arr['created_at'] = time();
                $arr['updated_at'] = time();
                $insert_arr[] = $arr;
            }
            $res = $this->CouponModel->addAll($insert_arr);
            $msg = doReturn("生成成功", "生成失败", $res);
            $this->ajaxReturn($msg);
        }
        $this->display();
    }

    public function send()
    {
        if (IS_POST) {
            $data = I('post.');
            $user = M('customer')->where(['mobile' => $data['data']['phone']])->find();
            if (empty($user)) {
                $msg = doReturn("添加成功", "用户不存在", false);
                $this->ajaxReturn($msg);
            }
            $info = $this->CouponTypeModel->find($data['data']['type']);
            $val = 0;
            if (!empty($info)) {
                $val = $info['expirationvalue'] * 86400 + time();
            } else {
                $msg = doReturn("数据异常", "用户不存在", false);
                $this->ajaxReturn($msg);
            }
            $res = $this->CouponModel->where(['id' => $data['id']])->save(['uid' => $user['customerid'], 'receive_at' => time(), 'expiration' => $val, 'status' => 2]);
            $msg = doReturn("发放成功", "发放失败", $res);
            $this->ajaxReturn($msg);
        }
    }

    // 发放
    public function editCoupon()
    {
        if (IS_POST) {
            $data = I('post.');
            $res = $this->CouponModel->where(['type' => $data['typeId']])->find();
            if (empty($res)) {
                $msg = doReturn("优惠券不足", "优惠券不足", false);
                $this->ajaxReturn($msg);
            }
        }
    }

    public function delCoupon()
    {
        if (IS_POST) {

        }
    }
}